package com.witframework.meta.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.witframework.meta.MetaApp;
import com.witframework.meta.MetaModule;
import com.witframework.meta.service.MetaAppService;
import com.witframework.meta.service.MetaModuleService;
import com.witframework.web.mvc.Controller;
import com.witframework.web.mvc.WebContext;

@Component
public class MetaModuleController implements Controller{

	@Autowired
	private MetaModuleService metaModuleService;
	@Autowired
	private MetaAppService metaAppService;
	
	@SuppressWarnings("unchecked")
	
	public String executor(WebContext webContext) {
		String app_id=webContext.getParameter("app_id");
		Map filter=webContext.getFilter();
		filter.put("app.id",app_id);
		
		List<MetaModule> moduleList = metaModuleService.pagedQuery(filter,webContext.getPageSortModel("module"));
		webContext.setAttribute("moduleList",moduleList);
		webContext.setAttribute("app_id",app_id);
		webContext.addFootmark("module_list"+app_id,"应用("+((MetaApp)metaAppService.get(app_id)).getName()+")");
		return WebContext.domainView("view/MetaModule/index.jsp");
	}
	public String edit(WebContext webContext){
		String[] ids=webContext.getSelItemList();
		if(ids!=null&&ids.length>0){
			MetaModule module=(MetaModule) metaModuleService.get(ids[0]);
			webContext.setAttribute("module", module);			
		}
		webContext.addNavigateTrace("module_edit","增加模块");
		webContext.setAttribute("app_id",webContext.getParameter("app_id"));
		return WebContext.domainView("view/MetaModule/edit.jsp");
	}

	public String store(WebContext webContext) {
		String app_id=webContext.getParameter("app_id");
		MetaModule module=new MetaModule();
		webContext.bindObject(module,"module_");
		module.generateRefCodeAndOther();
		module.setMetaApp((MetaApp)metaAppService.get(app_id));
		webContext.validateObject("module", module,WebContext.domainView("view/MetaModule/edit.jsp"));
		
		metaModuleService.saveOrUpdate(module);
		webContext.setPageInfo("数据保存成功.<script>window.parent.frames.menu.refreshTree(\"app_"+app_id+"\");</script>");
		String target=webContext.getTraceNavigate("module_edit");
		webContext.sendRedirect(target==null?"/metaModuleController.wit?app_id="+app_id:target);
		return null;
		

	}
	public String delete(WebContext webContext){
		metaModuleService.deleteAllByIds(webContext.getSelItemList());
		webContext.setPageInfo("成功删除.<script>window.parent.frames.menu.refreshTree(\"0\");</script>");
		return "redirect://this?app_id="+webContext.getParameter("app_id");
	}
	

}
